<template>
	<el-drawer v-model="visible" :title="titleMap[mode]" :size="800" append-to-body destroy-on-close
		@closed="$emit('closed')">
		<el-container>
			<el-main v-loading="dataloading">
				<div style="padding: 0px 30px">
					<el-form ref="dialogForm" :model="form" :rules="rules" :disabled="mode == 'show'"
						label-width="120px" label-position="left">
						<!-- 第一行 -->
						<labelnam title="基本信息"></labelnam>
						<el-row>
							<el-col :span="12">
								<el-form-item prop="empNum">
									<template #label>
										<span style="text-align: right">员工编码</span>
									</template>
									<el-input v-model="form.empNum" disabled placeholder="请输入员工编码" clearable></el-input>
								</el-form-item>
							</el-col>
							<!-- <el-col :span="2"> </el-col> -->

							<el-col :span="12">
								<el-form-item label="在职状态" prop="status">
									<el-select v-model="form.status" placeholder="请选择状态" disabled style="width: 400px">
										<el-option v-for="(option, index) in statelist" :key="index"
											:label="option.name" :value="option.status">
										</el-option>
									</el-select>
									<!-- <el-input disabled v-model="form.status" placeholder="请输入状态" clearable></el-input> -->
								</el-form-item>
							</el-col>
							<el-col :span="12">
								<el-form-item label="姓名" prop="name">
									<el-input v-model="form.name" disabled placeholder="请输入姓名" clearable></el-input>
								</el-form-item>
							</el-col>
							<!-- <el-col :span="2"> </el-col> -->
							<el-col :span="12">
								<el-form-item label="性别" prop="sex">
									<el-input v-model="form.sex" disabled placeholder="请输入性别" clearable></el-input>
								</el-form-item>
							</el-col>
						</el-row>

						<!-- 第二行开始 -->
						<el-row> </el-row>

						<el-row>
							<el-col :span="12">
								<el-form-item label="LDAP" prop="ldap">
									<el-input v-model="form.ldap" disabled placeholder="请输入LDAP" clearable></el-input>
								</el-form-item>
							</el-col>

							<!-- <el-col :span="2"> </el-col> -->

							<el-col :span="12">
								<el-form-item label="职位" prop="post">
									<el-input v-model="form.post" disabled placeholder="请输入职位" clearable></el-input>
								</el-form-item>
							</el-col>
							<!--							<el-col :span="10">-->
							<!--								<el-form-item label="电话" prop="phone">-->
							<!--									<el-input disabled v-model="form.phone" placeholder="请输入电话" clearable></el-input>-->
							<!--								</el-form-item>-->
							<!--							</el-col>-->
						</el-row>

						<el-row>
							<el-col :span="12">
								<el-form-item label="部门" prop="dept">
									<el-input v-model="form.dept" disabled placeholder="请输入部门" clearable></el-input>
								</el-form-item>
							</el-col>

							<el-col :span="10">
								<el-form-item label="岗位" prop="post">
									<el-input v-model="form.post" disabled placeholder="请输入岗位" clearable></el-input>
								</el-form-item>
							</el-col>


							<!-- <el-col :span="2"> </el-col> -->
							<el-col :span="12">
								<el-form-item label="邮箱" prop="email">
									<el-input v-model="form.email" disabled placeholder="请输入邮箱" clearable></el-input>
								</el-form-item>
							</el-col>

							<el-col :span="12">
								<el-form-item label="公司" prop="companyDescr">
									<el-input v-model="form.companyDescr" disabled placeholder="请输入公司"
										clearable></el-input>
								</el-form-item>
							</el-col>
							<!-- <el-col :span="12">
								<el-form-item label="部门" prop="dept">
									<el-input v-model="form.dept" disabled placeholder="请输入部门" clearable></el-input>
								</el-form-item>
							</el-col> -->

							<!-- <el-col :span="2"> </el-col> -->
						</el-row>

						<el-row>
							<!-- <el-col :span="10">
								<el-form-item label="岗位" prop="post">
									<el-input v-model="form.post" disabled placeholder="请输入岗位" clearable></el-input>
								</el-form-item>
							</el-col> -->
							<el-col :span="12">
								<el-form-item label="上级编码" prop="empNumPid">
									<el-input v-model="form.empNumPid" disabled placeholder="请输入直接上级编码"
										clearable></el-input>
								</el-form-item>

							</el-col>

							<el-col :span="12">
								<el-form-item label="上级姓名" prop="empPname">
									<el-input v-model="form.empPname" disabled placeholder="请输入直接上级名称"
										clearable></el-input>
								</el-form-item>
							</el-col>
							<!-- <el-col :span="1"> </el-col> -->
							<!-- <el-col :span="12">
								<el-form-item label="公司" prop="companyDescr">
									<el-input v-model="form.companyDescr" disabled placeholder="请输入公司"
										clearable></el-input>
								</el-form-item>
							</el-col> -->
							<!-- <el-col :span="2"> </el-col> -->
							<!-- <el-col :span="10">
								<el-form-item label="部门" prop="dept">
									<el-input v-model="form.dept" disabled placeholder="请输入部门" clearable></el-input>
								</el-form-item>
							</el-col> -->

							<!-- <el-col :span="10">
								<el-form-item label="直接上级编码" prop="empNumPid">
									<el-input v-model="form.empNumPid" disabled placeholder="请输入直接上级编码" clearable></el-input>
								</el-form-item>
							</el-col> -->
							<!-- <el-col :span="2"> </el-col> -->
						</el-row>

						<el-row>

							<!-- <el-col :span="12">
								<el-form-item label="上级姓名" prop="empPname">
									<el-input v-model="form.empPname" disabled placeholder="请输入直接上级名称"
										clearable></el-input>
								</el-form-item>
							</el-col> -->
							<!-- <el-col :span="1"> </el-col> -->

							<!-- <el-col :span="12">
								<el-form-item label="岗位" prop="posn">
									<el-input v-model="form.posn" disabled placeholder="请输入公司" clearable></el-input>
								</el-form-item>
							</el-col> -->

						</el-row>


						<!-- <div class="data-permissions">数据权限设置</div> -->
						<labelnam title="数据权限设置"></labelnam>

						<!-- 修改为多选下拉列表开始 -->
						<!-- <labelnam title="基本信息"></labelnam> -->
						<el-row>
							<el-col :span="24">
								<el-form-item label="负责片区" prop="areaDept">
									<!-- <el-select v-model="form.areaDept" placeholder="请选择负责片区" style="width: 400px">
										<el-option v-for="(option, index) in areaDeptOptions" :key="index"
											:label="returnData(option.label)" :value="option.num">
										</el-option>
									</el-select> -->

									<el-cascader v-model="form.areaDept" :options="areaDeptData"
										:props="{ multiple: false, checkStrictly: true }" clearable>
									</el-cascader>


								</el-form-item>
							</el-col>

							<el-col :span="24">
								<!-- <el-form-item label="对应场景部" prop="sceneList"> -->
								<el-form-item label="负责场景" prop="sceneList">
									<!-- <el-select v-model="form.sceneList" placeholder="请选择对应场景部" multiple
										style="width: 400px" filterable @change="sceneDept" collapse-tags>
										<el-option v-for="(option, index) in scene" :key="index"
											:label="returnData(option.orgName)" :value="option.orgNo">
										</el-option>
									</el-select> -->

									<el-cascader v-model="form.sceneList" :options="sceneData"
										:props="{ multiple: true, checkStrictly: true }" clearable>
									</el-cascader>


								</el-form-item>
							</el-col>
						</el-row>
						<!-- 修改为多选下拉列表结束 -->
						<el-row>
							<el-col :span="24">
								<!-- <el-form-item label="省份" prop="provList"> -->
								<el-form-item label="负责地区" prop="provList">

									<!-- <el-tree-select  v-if="loading" ref="seltree" collapse-tags v-model="form.provList"
										:data="options" style="width: 400px" show-checkbox multiple
										:show-all-levels="false" clearable separator=" / " placeholder="请选择省份/城市"
										@check-change="handleCheckChange">
									</el-tree-select>
									<el-select v-else suffix-icon="Loading" placeholder="请选择省份/城市" style="width: 100%">
										<template #prefix>
											<el-icon class="icon" size="18px" color="#000">
												<el-icon-Loading />
											</el-icon>
										</template>
									</el-select> -->

									<!-- <el-cascader ref="seltree" :options="options" v-model="form.provList"
										:props="{ multiple: true }" clearable style="width: 400px">
									</el-cascader> -->
									<div style="width: 100%;height: 200px;overflow: auto;">
										<el-cascader v-if="loading" ref="seltree" :options="options"
											v-model="form.provList" :props="{ multiple: true }" clearable
											style="width: 400px" :formatter="formatter"
											@check-change="handleCheckChange">
										</el-cascader>
										<el-select v-else suffix-icon="Loading" placeholder="请选择省份/城市"
											style="width: 100%">
											<template #prefix>
												<el-icon class="icon" size="18px" color="#000">
													<el-icon-Loading />
												</el-icon>
											</template>
										</el-select>
									</div>


								</el-form-item>
							</el-col>
						</el-row>
						<el-row>
							<!-- <el-col :span="24">
							  <el-divider orientation="left">分配客户</el-divider>
							</el-col> -->
						</el-row>

						<el-row>
							<!-- <el-col :span="2">
								<el-button type="primary" icon="el-icon-plus" size="small" @click="handleClick">fen</el-button>
							</el-col> -->
							<!-- <el-col :span="22">
								<el-table :data="allocatedCustomers" style="width: 100%" stripe>
								<el-table-column prop="id" label="客户ID"></el-table-column>
								<el-table-column prop="name" label="客户名称"></el-table-column>
								<el-table-column prop="phone" label="客户电话"></el-table-column>
								<el-table-column prop="email" label="客户邮箱"></el-table-column>
								<el-table-column prop="address" label="客户地址"></el-table-column>
								</el-table>
							</el-col> -->
						</el-row>
					</el-form>
				</div>
			</el-main>
			<el-footer>
				<el-button style="margin-left: 30px" @click="visible = false">取 消</el-button>
				<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
			</el-footer>
			<!-- <div style="width: 100%;height: 70px;position: absolute;bottom: 0;border-top: 1px solid #E8E8E8;padding-top: 10px">
				<el-button style="margin-left: 30px" @click="visible=false" >取 消</el-button>
				<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
			</div> -->
			<customer v-if="customerVisible" ref="customer" @success="handleSuccess" @closed="customerVisible = false"
				@guanbi="close"></customer>
		</el-container>
	</el-drawer>
</template>
<script>
import customer from './customer.vue'
import labelnam from './labelname.vue'

export default {
	components: { customer, labelnam },
	emits: ['success', 'closed'],
	data() {
		return {
			searchValue: '',
			mode: 'add',
			titleMap: {
				edit: '编辑用户',
				show: '查看'
			},
			loading: false,
			dataloading: false,
			options: [],
			visible: false,
			customerVisible: false, //
			isSaveing: false,
			scene: [],
			statelist: [
				{
					status: '0',
					name: '在职'
				},
				{
					status: '1',
					name: '离职'
				}
			],
			form: {
				empNum: '',
				status: '',
				name: '',
				phone: '',
				ldap: '',
				email: '',
				companyDescr: '',
				dept: '',
				post: '', // 修改为数组类型
				postLevel: '',
				sceneDept: [],
				areaDept: '',
				province: [],
				sceneList: [],
				empArea: {},
				provList: [],
				empDepartment: [],
				empPname: '',
				posn: ''
			},
			scenedata: [],
			rules: {
				posn: {
					required: true,
					message: '岗位不能为空',
					trigger: 'blur'
				},
				empPname: {
					required: true,
					message: '直接上级名称不能为空',
					trigger: 'blur'
				},
				empNum: {
					required: true,
					message: '编码不能为空',
					trigger: 'blur'
				},
				status: {
					required: true,
					message: '状态不能为空',
					trigger: 'blur'
				},
				name: {
					required: true,
					message: '姓名不能为空',
					trigger: 'blur'
				},
				phone: {
					required: false,
					message: '电话不能为空',
					trigger: 'blur'
				},
				ldap: {
					required: true,
					message: 'LDAP不能为空',
					trigger: 'blur'
				},
				email: {
					required: false,
					message: '邮箱不能为空',
					trigger: 'blur'
				},
				companyDescr: {
					required: false,
					message: '公司不能为空',
					trigger: 'blur'
				},
				dept: {
					required: true,
					message: '部门不能为空',
					trigger: 'blur'
				},
				post: {
					required: true,
					message: '岗位不能为空',
					trigger: 'blur'
				},
				postLevel: {
					required: true,
					message: '岗位级别不能为空',
					trigger: 'blur'
				},
				sceneList: {
					required: true,
					message: '对应场景部不能为空',
					trigger: 'blur'
				},
				areaDept: {
					required: true,
					message: '负责片区不能为空',
					trigger: 'blur'
				},
				// 修改传参位置empProvinces empProvinces
				provList: {
					required: true,
					message: '省份不能为空',
					trigger: 'blur'
				}
			},
			groups: [],
			groupsProps: {
				value: 'id',
				emitPath: false,
				checkStrictly: true
			},
			depts: [],
			deptsProps: {
				value: 'id',
				checkStrictly: true
			},
			areaDeptOptions: [],
			sceneData: [],
			areaDeptData: []

		}
	},
	mounted() {
		this.getGroup()
		this.getDept()
		this.getChinaTree()
		// this.options = this.$TOOL.data.get('ChinaTree')
	},
	methods: {
		formatter(value, selectedOptions) {
			// 返回用于显示的值
			return "显示的值";
		},

		newScene() {
			this.sceneData = this.scene.map(item => {
				return {
					label: item.orgName == '场景部' ? '全部' : item.orgName,
					value: item.orgNo,
					children: this.formatChildren(item.children)
				};
			});

			this.areaDeptData = this.areaDeptOptions.map(item => {
				return {
					label: item.label == '片区' ? '全部' : item.label,
					value: item.num,
					children: this.formatChildren2(item.children)
				};
			});
		},

		formatChildren(children) {
			if (!children || children.length === 0) {
				return null;
			}
			return children.map(child => {
				return {
					label: child.orgName,
					value: child.orgNo,
					children: this.formatChildren(child.children)
				};
			});
		},

		formatChildren2(children) {
			if (!children || children.length === 0) {
				return null;
			}
			return children.map(child => {
				return {
					label: child.label,
					value: child.num,
					children: this.formatChildren2(child.children)
				};
			});
		},

		returnData(e) {
			if (e == '片区' || e == '场景部') {
				return '全部'
			} else {
				return e
			}
		},

		async getChinaTree() {
			let list = []
			var res = await this.$API.emp.getChinaTree.post({ data: {} })
			if (res.code == '0') {
				list = res.data.map((province) => ({
					label: province.province,
					value: province.code,
					tag: 0,
					children: province.cities?.map((city) => ({
						// label: province.province + '/' + city.name,
						label: city.name,
						value: city.code,
						code: province.code,
						labels: city.name,
						tag: 1,
						province: province.province,
						children: city.area?.map((area) => ({
							// label: province.province + '/' + city.name + '/' + area.name,
							label: area.name,
							value: area.code,
							code: province.code,
							province: province.province,
							labels: city.name,
							citycode: city.code,
							areaName: area.name,
							areacode: area.code,
							tag: 2
						}))
					}))
				}))
			}

			if (this.form.returnEmpProvinces) {
				this.form.provList = []
				this.form.returnEmpProvinces.forEach((item) => {
					if (item.cityName == 'all' && item.cityCode == 'all') {
						list.forEach((item1) => {
							if (item.provinceCode == item1.value) {
								item1.children.forEach((item2) => {
									if (item2.children?.length > 0) {
										item2.children.forEach((item3) => {
											this.form.provList.push(Number(item3.value))
										})
									} else {
										this.form.provList.push(Number(item2.value))
									}
								})
							}
						})
					} else if (
						item.cityName != 'all' &&
						item.cityCode != 'all' &&
						item.areaName == 'all' &&
						item.areaCode == 'all'
					) {
						list.forEach((item1) => {
							if (item.provinceCode == item1.value) {
								item1.children.forEach((item2) => {
									if (item.cityCode == item2.value) {
										if (item2.children?.length > 0) {
											item2.children.forEach((item3) => {
												this.form.provList.push(Number(item3.value))
											})
										} else {
											this.form.provList.push(Number(item2.value))
										}
									}
								})
							}
						})
					} else {
						this.form.provList.push(Number(item.areaCode))
					}
				})
			}

			// console.log('数据',this.form.returnEmpProvinces);
			// this.form.provList = [110101]
			// console.log('地区数据', this.form.provList);
			this.loading = true
			this.options = list
		},
		dialogClick() {
			this.$nextTick(() => {
				this.$refs.customer.multipleSelection
			})
		},
		sceneDept(val) {
			if (val.length > 0) {
				let arr = []
				val.forEach((item) => {
					this.scene.forEach((item1) => {
						if (item == item1.orgNo) {
							arr.push(item1)
						}
					})
				})
				this.scenedata = arr
			}
		},
		getSimpleCheckedNodeIds(originData) {
			// 定义数组
			const checkedNodeIds = []
			// 判断是否为全选，若为全选状态返回被全选的节点，不为全选状态正常返回被选中的节点
			const isAllChecked = function (node) {
				const childNodes = node.root ? node.root.childNodes : node.childNodes
				childNodes.forEach((child) => {
					if (child.checked) {
						checkedNodeIds.push(child.data.id)
					}
					if (child.indeterminate) {
						isAllChecked(child)
					}
				})
			}
			isAllChecked(originData)
			return checkedNodeIds
		},
		// handleClick() {
		// 	this.$refs.dialogForm.validate(async (valid) => {
		// 		if (!valid) {
		// 			// this.form.sceneDept = this.form.sceneList
		// 			// this.form.areaDept = this.form.empArea
		// 			// this.form.province = this.form.provList
		// 			this.customerVisible = true
		// 			this.$nextTick(() => {
		// 				if (this.form.sceneList.length > 0) {
		// 					let arr = []
		// 					this.form.sceneList.forEach((item) => {
		// 						this.scene.forEach((item1) => {
		// 							if (item == item1.orgNo) {
		// 								arr.push(item1)
		// 							}
		// 						})
		// 					})
		// 					this.form.empDepartment = arr
		// 				}
		// 				// 省市
		// 				if (this.$refs.seltree.getCheckedNodes().length > 0) {
		// 					let name = ''
		// 					let arr = []
		// 					this.$refs.seltree.getCheckedNodes().forEach((item) => {
		// 						if (item.children) {
		// 							name = item.value
		// 							let obj = {
		// 								provinceName: item.label,
		// 								provinceCode: item.value,
		// 								cityCode: 'all',
		// 								cityName: 'all'
		// 							}
		// 							arr.push(obj)
		// 						} else if (item.code != name) {
		// 							let obj = {
		// 								provinceName: item.province,
		// 								provinceCode: item.code,
		// 								cityCode: item.value,
		// 								cityName: item.labels
		// 							}
		// 							arr.push(obj)
		// 						}
		// 					})
		// 					this.form.empProvinces = arr
		// 				}
		// 				let data = { ...this.form }
		// 				// 片区
		// 				data.empArea = {}
		// 				this.areaDeptOptions.forEach((item) => {
		// 					if (data.areaDept == item.orgNo) {
		// 						data.empArea = item
		// 					}
		// 				})

		// 				// data.sceneDept.forEach(item=>{
		// 				// 	data.department += item+';'
		// 				// })
		// 				this.$refs.customer.open('customer').setData(data)
		// 			})
		// 		} else {
		// 			return false
		// 		}
		// 	})
		// },
		handleCheckChange(e) {
			// console.log(99,e);
			console.log('数据99999', e);
			// console.log('地区数据2',this.form.provList);


		},
		open(mode) {
			this.mode = mode
			this.visible = true
			return this
		},
		handleNodeClick(node) { },
		async getGroup() {
			var res = await this.$API.basic.org.list.post({ data: {} })
			this.groups = res.data
			var scenes = await this.$API.emp.getScene.get()
			this.scene = scenes.data
			var area = await this.$API.emp.getArea.get()
			this.areaDeptOptions = area.data


			this.newScene()
		},
		async getDept() {
			var res = await this.$API.basic.org.list.post({ data: {} })
			this.depts = res.data
		},
		// async getChinaTree(){
		//      var res = await this.$API.emp.getChinaTree.post();
		// 	 if(res.code == '0'){
		// 		this.options = res.data.map(province => ({
		// 		label: province.province,
		// 		value: province.code,
		// 		tag:0,
		// 		children: province.cities.map(city =>({
		// 			label: province.province+'/'+city.name,
		// 			value: city.code,
		// 			code:province.code,
		// 			labels:city.name,
		// 			tag:1,
		// 			province:province.province,
		// 			children:city.area.map(area => ({
		// 		        label: province.province+'/'+city.name+'/'+area.name,
		// 		    	value: area.code,
		// 			    code:province.code,
		// 				 province:province.province,
		// 			    labels:city.name,
		// 			    citycode:city.code,
		// 				areaName:area.name,
		// 				areacode:area.code,

		// 				tag:2
		// 			}))
		// 		}))
		// 	}));
		// 	 }
		// },
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true
					//    场景部
					if (this.form.sceneList.length > 0) {
						let arr = []
						this.form.sceneList.forEach((item) => {
							if (typeof (item) == 'string') {
								this.scene.forEach((item1) => {
									if (item == item1.orgNo) {
										arr.push(item1)
									}
									if (item1.children) {
										item1.children.forEach((e) => {
											if (item == e.orgNo) {
												arr.push(e)
											}
										})
									}
								})
							} else {
								item.forEach((item2) => {
									this.scene.forEach((item1) => {
										if (item2 == item1.orgNo) {
											arr.push(item1)
										}
										if (item1.children) {
											item1.children.forEach((e) => {
												if (item2 == e.orgNo) {
													console.log('触发了子集', e);
													arr.push(e)
												}
											})
										}
									})

								})
							}


							// this.scene.forEach((item1) => {
							// 	if (item == item1.orgNo) {
							// 		arr.push(item1)
							// 	}

						})
						this.form.empDepartment = arr
					} else {
						this.form.empDepartment = []
					}


					console.log('选择的地区', this.$refs.seltree.getCheckedNodes());


					// 省市                      OTC事业部-数字化转型办公室
					// if (this.form.dept.includes('OTC事业部-数字化转型办公室')) {
					if (this.$refs.seltree.getCheckedNodes().length > 0) {
						let provinceName = ''
						let cityName = ''
						let arr = []
						// this.$refs.seltree.getCheckedNodes().forEach((item) => {
						// 	if (item.tag == 0) {
						// 		provinceName = item.value
						// 		let obj = {
						// 			provinceName: item.label,
						// 			provinceCode: item.value,
						// 			cityCode: 'all',
						// 			cityName: 'all',
						// 			areaCode: 'all',
						// 			areaName: 'all'
						// 		}
						// 		arr.push(obj)
						// 	} else if (item.tag == 1 && item.code != provinceName) {
						// 		cityName = item.value
						// 		let obj = {
						// 			provinceName: item.province,
						// 			provinceCode: item.code,
						// 			cityCode: item.value,
						// 			cityName: item.labels,
						// 			areaCode: 'all',
						// 			areaName: 'all'
						// 		}
						// 		arr.push(obj)
						// 	} else if (item.tag == 2 && item.code != provinceName && item.citycode != cityName) {
						// 		let obj = {
						// 			provinceName: item.province,
						// 			provinceCode: item.code,
						// 			cityCode: item.citycode,
						// 			cityName: item.labels,
						// 			areaCode: item.areacode,
						// 			areaName: item.areaName
						// 		}
						// 		arr.push(obj)
						// 	}
						// })

						this.$refs.seltree.getCheckedNodes().forEach((item) => {

							if (item.level == 1) {
								provinceName = item.value
								let obj = {
									provinceName: item.label,
									provinceCode: item.value,
									cityCode: 'all',
									cityName: 'all',
									areaCode: 'all',
									areaName: 'all'
								}
								arr.push(obj)
							} else if (item.level == 2 && item.parent.value != provinceName) {
								cityName = item.value
								let obj = {
									provinceName: item.parent.label,
									provinceCode: item.parent.value,
									cityCode: item.value,
									cityName: item.label,
									areaCode: 'all',
									areaName: 'all'
								}
								arr.push(obj)
							} else if (item.level == 3 && item.parent.parent.value != provinceName && item.parent.value != cityName) {
								let obj = {
									provinceName: item.parent.parent.label,
									provinceCode: item.parent.parent.value,
									cityCode: item.parent.value,
									cityName: item.parent.label,
									areaCode: item.value,
									areaName: item.label
								}
								arr.push(obj)
							}


						})

						this.form.empProvinces = arr
					}
					// } else {
					// 	this.form.empProvinces = []
					// }

					this.form.department = []
					let data = { ...this.form }

					let newprovList = []
					data.provList.forEach((item) => {
						newprovList.push(item[item.length - 1])
						if (item[item.length - 1] == null) {
							let a = this.changeData(item[item.length - 2])
							a?.forEach((e) => {
								newprovList.push(e.value)
							})
						}
					})

					data.provList = [...new Set(newprovList)]

					// 片区
					data.empArea = {}
					data.areaDept = [data.areaDept[data.areaDept.length - 1]]
					// this.areaDeptOptions.forEach((item) => {
					// 	if (data.areaDept == item.num) {
					// 		data.empArea = { orgNo: item.num, orgName: item.label }
					// 	}
					// })
					this.areaDeptOptions.forEach((item, index) => {
						// data.empArea = { orgNo: item.num, orgName: item.label }
						if (data.areaDept[0] == item.num) {
							data.empArea = { orgNo: item.num, orgName: item.label }
						}
						if (item.children && item.children.length > 0) {
							item.children.forEach((e) => {
								if (data.areaDept[0] && data.areaDept[0] == e.num) {
									data.empArea = { orgNo: e.num, orgName: e.label }
								}
							})
						}



					})

					var res = await this.$API.emp.updateEmp.post({ data: data })
					this.isSaveing = false
					if (res.code == 0) {
						this.$emit('success')
						this.visible = false
						this.$message.success('操作成功')
					} else {
						this.$alert(res.message, '提示', { type: 'error' })
					}
				} else {
					return false
				}
			})
		},
		async setData(data) {
			this.dataloading = true
			var res = await this.$API.emp.getEmpDetails.post({ data: { empNum: data.empNum } })
			if (res.code == '0') {
				this.dataloading = false

				if (res.data.returnEmpArea) {
					this.form.areaDept = String(res.data.returnEmpArea.orgNo)
				}

				if (res.data.returnEmpDepartment) {
					this.form.sceneList = []
					res.data.returnEmpDepartment.forEach((item) => {
						this.form.sceneList.push(String(item.orgNo))
					})
				}


				this.form.provList = []

				console.log('数据', data);
				// if (data.returnEmpProvinces) {
				// 	this.form.empProvinces = []
				// 	data.returnEmpProvinces.forEach(item => {
				// 		if (item.cityName == 'all' && item.cityCode == 'all') {
				// 			this.options.forEach(item1 => {
				// 				if (item.provinceCode == item1.value) {
				// 					item1.children.forEach(item2 => {
				// 						if (item2.children.length > 0) {
				// 							item2.children.forEach(item3 => {
				// 								this.form.empProvinces.push(item3.value)
				// 							})
				// 						} else {
				// 							this.form.empProvinces.push(item2.value)
				// 						}

				// 					})
				// 				}
				// 			})
				// 		} else if (item.cityName != 'all' && item.cityCode != 'all' && item.areaName == 'all' && item.areaCode == 'all') {
				// 			this.options.forEach(item1 => {
				// 				if (item.provinceCode == item1.value) {
				// 					item1.children.forEach(item2 => {
				// 						if (item.cityCode == item2.value) {
				// 							if (item2.children.length > 0) {
				// 								item2.children.forEach(item3 => {
				// 									this.form.empProvinces.push(item3.value)
				// 								})
				// 							} else {
				// 								this.form.empProvinces.push(item2.value)
				// 							}

				// 						}

				// 					})
				// 				}
				// 			})
				// 		} else {
				// 			this.form.empProvinces.push(item.cityCode)
				// 		}
				// 	})
				// }
				this.rules.sceneList.required = res.data.dept.includes('OTC事业部-区域')
				this.rules.areaDept.required = res.data.dept.includes('OTC事业部-区域')
				this.rules.provList.required = res.data.dept.includes('OTC事业部-区域')
				Object.assign(this.form, res.data)
				// console.log(this.form)


			}
		},

		findChildrenByValue(children, targetValue) {
			for (let i = 0; i < children.length; i++) {
				const child = children[i];
				if (child.value === targetValue) {
					return child.children || [];
				} else if (child.children) {
					const foundInChildren = this.findChildrenByValue(child.children, targetValue);
					if (foundInChildren) {
						return foundInChildren;
					}
				}
			}
			return null;
		},

		changeData(e) {
			for (let i = 0; i < this.options.length; i++) {
				const item = this.options[i];
				if (item.value === e) {
					console.log(item.children);
					return item.children || [];
				} else if (item.children) {
					// 递归实现
					const foundInChildren = this.findChildrenByValue(item.children, e);
					if (foundInChildren) {
						return foundInChildren;
					}
				}
			}
		}


	}
}
</script>

<style scoped>
/deep/ .el-form--label-left .el-form-item__label {
	justify-content: flex-end;
}

/deep/ .el-select {
	width: 100%;
}

.row-box {
	display: flex;
	flex-direction: column;
}

.data-permissions {
	font-size: 16px;
	padding-top: 20px;
	padding-left: 50px;
	padding-bottom: 20px;
}

.icon {
	animation: rotating 2s linear infinite;
}

@keyframes icon {
	from {
		transform: rotate(0deg);
	}

	to {
		transform: rotate(360deg);
	}
}
</style>
